<template>
  <page-view :full="true">
    <top-bar title="产品详情"
             :white="true">
      <!-- <div>取消订单</div> -->
    </top-bar>
    <page-content class="p-details">
      <div class="d-header">
        <!-- info -->
        <van-skeleton avatar :row="2" :loading="initLoading" class="top-skeleton">
          <p class="p-name">{{info.name}}</p>
          <div class="p-btnBox">
            <span v-for="(label, index) in info.label" :key="index">{{label}}</span>
            <span class="p-right">{{info.property_right_cycle}}</span>
          </div>
        </van-skeleton>
        <van-skeleton :row="4" :loading="initLoading">
          <div class="p-day">
            <div class="p-price">{{info.price}}</div>
            <div class="p-db">
              <p class="p-zq">产品周期</p>
              <p class="p-dt">{{info.property_right_cycle}}天</p>
            </div>
          </div>
          <van-progress pivot-text="橙色"
                        color="#f2826a"
                        :percentage="info.surplus/info.stock * 100"
                        class="p_press"
                        :show-pivot="false" />
          <p class="p-sy"><span>剩余份数 {{info.surplus}}份</span><span>单价：{{info.price}} CNY一份</span></p>
        </van-skeleton>
      </div>
      <div class="p-coin">
        <p class="p-rule"><span>产出规则</span></p>
        <van-skeleton title :row="2" :loading="initLoading">
          <div class="p-time">
            <div class="p-tdetail first">
              <p>{{today}}</p>
              <p class="p-gmr">购买日</p>
            </div>
            <div class="p-tdetail dm">
              <p>{{periodDate}}</p>
              <p class="p-gmr">上架时间</p>
            </div>
            <div class="p-tdetail dt">
              <p>{{longDate}}</p>
              <p class="p-gmr dt">最长周期</p>
            </div>
          </div>
          <van-progress color="#D3D4FF"
                        track-color="#585DF9"
                        :percentage="info.shelf_period/info.property_right_cycle * 100"
                        class="p_press"
                        :show-pivot="false" />
        </van-skeleton>
        <van-tabs color="#585DF9"
                  line-width="29px"
                  title-active-color="#585DF9"
                  title-inactive-color="#3D3E54"
                  class="p-tabs">
          <van-tab title="产品规格">
            <van-skeleton title :row="3" :loading="initLoading" class="cpbox-skeleton">
              <div class="p-cpbox">
                <div class="p-line">
                  <span class="p-first">设备型号</span>
                  <span class="p-sec">{{info.name}}</span>
                  <span class="p-three">产出币种</span>
                  <span class="p-for">{{info.currency == 0 ? 'BZZ' : 'XCH'}}</span>
                </div>
                <div class="p-line">
                  <span class="p-first">额定空间</span>
                  <span class="p-sec">{{info.rated_space}} TiB</span>
                  <span class="p-three">有效空间</span>
                  <span class="p-for">{{info.effective_space_ratio}} %</span>
                </div>
                <div class="p-line">
                  <span class="p-first">存储时间</span>
                  <span class="p-sec">{{info.storage}} 天</span>
                  <span class="p-three">技术服务费</span>
                  <span class="p-for">{{info.technical_rate}} %</span>
                </div>
                <div class="p-line">
                  <span class="p-first">服务周期</span>
                  <span class="p-five">{{info.property_right_cycle}} 天</span>
                </div>
              </div>
              <p class="p-zysx">注意事项！</p>
              <p class="p-hy" v-html="info.risk"></p>
            </van-skeleton>
          </van-tab>
          <van-tab title="产品详情">
            <van-skeleton title :row="3" :loading="initLoading">
              <div class="p-cpboxs">
                <p class="p-p" v-html="info.introduce"></p>
              </div>
            </van-skeleton>
          </van-tab>
        </van-tabs>
        <van-button v-if="user"  
          round
          type="info"
          class="p-btn"
          @click="goPay">立即购买</van-button>
        
        <van-button v-if="!user" 
          round
          type="info"
          class="p-btn"
          @click="goLogin">立即登录购买</van-button>
      </div>
    </page-content>
    <van-action-sheet v-model="showAction"
                      :title="actionTitle"
                      class="p-action">
      <div class="content">
        <div class="fx cycle">
          <span class="title">租赁期限<span class="cyc-val">{{info.property_right_cycle}}天</span></span>
        </div>
        <div class="fx part">
          <span class="title">购买份数</span>
          <van-stepper v-model="partValue"
                      min="1"
                      :max="info.surplus"
                      input-width="40px"
                      button-size="38px"
                      @change="partChange" />
        </div>
        <!-- <div class="serve">
          <span class="title">服务周期</span>
          <div class="serve-list">
            <van-button round
                        plain
                        type="info"
                        @click="serveChange(0)">{{info.property_right_cycle}}天</van-button>
            <van-button round
                        :plain="serveValue != 1"
                        type="info"
                        @click="serveChange(1)">540天</van-button>
            <van-button round
                        :plain="serveValue != 2"
                        type="info"
                        @click="serveChange(2)">1024天</van-button>
          </div>
        </div> -->
        <div class="fx buy">
          <div>
            <span class="title">总价</span>
            <span class="price">{{info.price * partValue | toFixed(2)}}</span>
          </div>
          <van-button round
                      type="info"
                      class="buy-btn"
                      @click="buyAgreement">立即购买</van-button>
        </div>
      </div>
    </van-action-sheet>
  </page-view>
</template>

<script>
import { Toast } from 'vant';
import TopBar from '@/components/TopBar';
import PageView from '@/components/PageView';
import PageContent from '@/components/PageContent';
import moment from 'moment';
import service from '@/utils/request';

const D = 'YYYY-MM-DD'

export default {
  components: {
    TopBar,
    PageView,
    PageContent
  },
  data () {
    return {
      showAction: false,
      actionTitle: "",
      // serveValue: 0,
      partValue: 1,
      initLoading: true,
      info: {},
      today: "",
      periodDate: "",
      longDate: "",
      user: {},
      id: ""
    }
  },
  created () {
    //根据key名获取传递回来的参数

  },
  destroyed () {

  },
  mounted(){
    this.id = this.$route.params.id;
    this.getData();
  },
  //每次激活时
  activated () {
    let backParams = this.$router.backParams();
    if (backParams) {
      if(backParams.order){
        this.showAction = false;
      }
      if(backParams.order == 2){
        this.orderCommit().then(e=>{
          this.partValue = 1;
        });
      }
    }
    if(this.id != this.$route.params.id){
      this.id = this.$route.params.id;
      this.initLoading = true;
      this.getData();
    }
    this.user = this.$store.state.user
  },
  destroyed () {
  },
  methods: {
    getData(){
      return service.post('/apiGetProductInfo.do', {
        id: this.id
      }).then(res=>{
        this.info = res.data;
        this.today = moment().format(D);
        this.periodDate = moment().add(this.info.shelf_period, 'day').format(D);
        this.longDate = moment().add(this.info.property_right_cycle, 'day').format(D);
        this.initLoading = false;
      })
    },
    // serveChange (v) {
    //   this.serveValue = v
    // },
    partChange (v) {
      this.partValue = v
    },
    goPay () {
      this.showAction = true;
      this.actionTitle = "存储服务器（分布式）A-1T";
    },
    goLogin () {
      this.$router.push({ path: `/login/1` })
    },
    buyAgreement () {
      this.$router.push({ path: '/product/agreement/RISK_AGREEMENT' })
    },
    orderCommit(){
      const toast = Toast.loading({
        duration: 0, // 持续展示 toast
        forbidClick: true,
        message: '订单生成中',
      });
      return service.post('/apiCreateOrder.do', {
        id: this.id,
        number: this.partValue
      }, {
        hideloading: true
      }).then(res=>{
        setTimeout(()=>{
          this.$router.replace({ 
            path: `/product/order/${res.data}`,
          })
          toast.clear();
        }, 1500)
      }, ()=>{
        toast.clear();
      })
    }
  },
}
</script>

<style lang="scss" scope>
.p-details {
  .cpbox-skeleton{
    margin-top: 15px;
  }
  .top-skeleton{
    margin-bottom: 10px;
  }
  p {
    margin: 0;
  }
  .d-tit {
    width: 100%;
    text-align: center;
    position: relative;
    top: 3px;
  }
  .detail-container {
    width: 100vw;
    min-height: 100vh;
  }
  .d-header {
    height: 200px;
    padding: 70px 20px 0 20px;
    background: #714b4b linear-gradient(141deg, #585df9 0%, #5885f9 100%);
  }
  .p-back {
    width: 22px;
    height: 22px;
    vertical-align: middle;
    position: absolute;
    left: 0;
  }
  .p-center {
    width: auto;
    height: 28px;
    font-size: 20px;
    font-weight: 500;
    color: #ffffff;
    line-height: 28px;
    margin: 0 auto;
    vertical-align: middle;
    display: inline-block;
  }
  .p-name {
    height: 20px;
    font-size: 14px;
    font-weight: 500;
    color: #ffffff;
    line-height: 20px;
    margin-top: 8px;
  }
  .p-btnBox {
    height: auto;
    margin-top: 9px;
    height: 45px;
    overflow: hidden;
  }
  .p-btnBox span {
    display: inline-block;
    margin-right: 10px;
    margin-bottom: 1px;
    padding: 3px 6px;
    border-radius: 10px;
    font-size: 10px;
    font-weight: 400;
    color: #ffffff;
    vertical-align: middle;
    background: #ff8445;
  }
  .p-right {
  }
  .p-day {
    height: 31px;
    line-height: 31px;
    display: flex;
    justify-content: space-between;
    margin-top: 5px;
    color: #fff;
  }
  .p-day .p-price {
    font-size: 20px;
  }
  .p-db {
    line-height: 15px;
  }
  .p-zq {
    font-size: 10px;
  }
  .p-dt {
    font-size: 12px;
  }
  .p_press {
    margin-top: 18px;
  }
  .p-sy {
    display: flex;
    justify-content: space-between;
    font-size: 10px;
    font-weight: 400;
    color: #ffffff;
    margin-top: 9px;
  }
  .p-coin {
    background: #ffffff;
    border-top-left-radius: 19px;
    border-top-right-radius: 19px;
    padding: 26px 20px 100px 21px;
    margin-top: -26px;
  }
  .p-rule {
    margin-bottom: 29px;
    height: 19px;
    line-height: 19px;
    font-size: 14px;
    font-weight: 500;
    color: #3d3e54;
    border-left: 3px solid #585df9;
  }
  .p-rule span {
    margin-left: 8px;
  }
  .p-time {
    height: auto;
    margin-bottom: 15px;
    display: flex;
    justify-content: space-between;
  }
  .p-tdetail {
    // width: 100px;
    width: auto;
    height: auto;
    display: inline-block;
    vertical-align: middle;
    font-size: 10px;
    color: #7a7886;
  }
  .first {
    margin-right: 10%;
  }
  .p-gmr {
    color: #3d3e54;
  }
  .p-coin .dm {
    margin-right: 30%;
  }
  .dt {
    color: #585df9;
    text-align: right;
  }
  .p-tabs {
    margin-top: 26px;
  }
  .van-tabs__wrap {
    width: 150px;
  }
  .p-cpbox {
    margin-top: 24px;
    // width: 335px;
    // margin: 24px 20px 0;
    box-sizing: border-box;
    border-top: 1px solid #979797;
    border-left: 1px solid #979797;
    // border-bottom: 1px solid #979797;
  }
  .p-cpboxs {
    margin-top: 24px;
  }
  .p-line {
    min-height: 45px;
    // line-height: 45px;
    box-sizing: border-box;
    border-bottom: 1px solid #979797;
    display: flex;
    flex: 1;
    justify-content: space-between;
    text-align: center;
  }
  .p-line span {    
    display: flex;
    justify-content: center;
    align-items: center;
    // height: 45px;
    padding: 10px 0;
    box-sizing: border-box;
    text-align: center;
    border-right: 1px solid #979797;
    text-align: center;
    font-size: 14px;
    color: #7a7886;
  }
  // <span class="p-first"></span>
  //             <span class="p-sec"></span>
  //             <span class="p-three"></span>
  //             <span class="p-for"></span>
  .p-first {
    background: #d8d8d8;
    width: 75px;
  }
  .p-sec{
    width: 136px;
  }
  .p-three {
    width: 75px;
    background: #d8d8d8;
  }
  .p-five {
    width: 254px;
    text-align: center;
  }
  .p-for{
    width: 45px;
  }
  .p-zysx {
    height: 17px;
    font-size: 12px;
    font-weight: 500;
    color: #585df9;
    line-height: 17px;
    margin-top: 18px;
  }
  .p-hy {
    font-size: 14px;
    font-weight: 400;
    color: #7a7886;
    line-height: 20px;
    margin-top: 8px;
  }
  .p-btn {
    width: 335px;
    height: 57px;
    line-height: 57px;
    background: #585df9;
    border-radius: 29px;
    position: fixed;
    font-size: 17px;
    font-weight: 500;
    color: #ffffff;
    bottom: 18px;
    text-align: center;
    margin: auto;
    left: 0;
    right: 0;
  }
  .p-p {
    height: auto;
    line-height: 20px;
    font-weight: 600px;
  }
}

.p-action {
  .van-action-sheet__header {
    display: flex;
    padding: 0 20px;
    font-weight: 600;
  }
  .van-stepper__input {
    font-size: 18px;
  }
  .content {
    // padding: 0 20px 10px;
    .fx {
      display: flex;
      justify-content: space-between;
      align-items: center;
      height: 50px;
    }
    .cycle {
      height: 40px;
      margin: 0 20px;
      margin-bottom: 10px;
      .cyc-val {
        margin-left: 10px;
        font-size: 16px;
        font-weight: 600;
        color: #585df9;
      }
    }
    .part {
      margin: 0 20px;
      margin-bottom: 10px;
    }
    .serve {
      margin: 0 20px;
      padding-bottom: 10px;
      border-bottom: 1px solid #e2e2e2;
    }
    .buy {
      padding: 10px 20px;
      .price {
        font-size: 24px;
        font-weight: 600;
        color: #585df9;
      }
      .buy-btn {
        width: 160px;
        height: 45px;
        background: #585df9;
        border-radius: 29px;
        background-color: #585df9;
        font-size: 17px;
        font-weight: 500;
        line-height: 45px;
        text-align: center;
        color: #ffffff;
      }
      .title {
        margin-right: 10px;
        font-size: 14px;
        color: #3d3e54;
        line-height: 20px;
      }
    }
  }
  .serve-list {
    display: flex;
    flex-flow: wrap;
    min-width: 75px;
    & > button {
      height: 38px;
      font-size: 16px;
      margin: 5px;
      &.van-button--info {
        background-color: #585df9;
        border: 1px solid #474cf7;
      }
      &.van-button--plain {
        background-color: #fff;
        color: #585df9;
      }
    }
  }
}
</style>
